var express = require('express');
var router = express.Router();
var path = require("path");
var mysql = require('mysql');

// 获取验证模块
var authorization = require(path.join(process.cwd(),"/modules/authorization"));

// 通过验证模块获取分类管理
var orderServ = authorization.getService("ProcessService");





// 订单列表
router.get("/",
	// 参数验证
	function(req,res,next) {
		// 参数验证
		if(!req.query.pagenum || req.query.pagenum <= 0) return res.sendResult(null,400,"pagenum 参数错误");
		if(!req.query.pagesize || req.query.pagesize <= 0) return res.sendResult(null,400,"pagesize 参数错误"); 
		next();
	},
	// 业务逻辑
	function(req,res,next) {
		var conditions = {
			"pagenum" : req.query.pagenum,
			"pagesize" : req.query.pagesize
		};
		
		if(req.query.query) {
			conditions["query"] = req.query.query;
		}

		if(req.query.user_id) {
			conditions["user_id"] = req.query.user_id;
		}
		if(req.query.yiqi_time) {
			conditions["yiqi_time"] = req.query.yiqi_time;
		}
		if(req.query.yiqi_id) {
			conditions["yiqi_id"] = req.query.yiqi_id;
		}
		if(req.query.yiqi_name) {
			conditions["yiqi_name"] = req.query.yiqi_name;
		}
		if(req.query.yiqi_zhuangtai) {
			conditions["yiqi_zhuangtai"] = req.query.yiqi_zhuangtai;
		}
		if(req.query.yiqi_xinghao) {
			conditions["yiqi_xinghao"] = req.query.yiqi_xinghao;
		}
		if(req.query.yiqi_chuchangbianhao) {
			conditions["yiqi_chuchangbianhao"] = req.query.yiqi_chuchangbianhao;
		}
		if(req.query.yiqi_yiqisongjiandanwei) {
			conditions["yiqi_yiqisongjiandanwei"] = req.query.yiqi_yiqisongjiandanwei;
		}
		if(req.query.yiqi_jiaozhunzhengshubh) {
			conditions["yiqi_jiaozhunzhengshubh"] = req.query.yiqi_jiaozhunzhengshubh;
		}
		if(req.query.yiqi_zhizaoshang) {
			conditions["yiqi_zhizaoshang"] = req.query.yiqi_zhizaoshang;
		}
		if(req.query.yiqi_chuanganqileixing) {
			conditions["yiqi_chuanganqileixing"] = req.query.yiqi_chuanganqileixing;
		}
		if(req.query.value1) {
			conditions["user_id"] = req.query.value1;
		}
		if(req.query.value2) {
			conditions["yiqi_songjiandanwei"] = req.query.value2;
		}
		if(req.query.value3) {
			conditions["yiqi_zhizaoshang"] = req.query.value3;
		}
		if(req.query.value4) {
			conditions["yiqi_zhuangtai"] = req.query.value4;
		}
		
		
		// 加载配置文件
		var config = require('config').get("db_config");
	
		var connection = mysql.createConnection({
			host     : config.get("host"),
			user     : config.get("user"),
			password : config.get("password"),
			database : config.get("database")
		});
		connection.connect();

		// 用来标记 0 表示获取的是所有列表   1 表示获取的是当前用户提交的列表
		global.flug = 0;
		var x = 0;
		connection.query("SELECT COUNT(*) as num FROM `sp_role` WHERE role_id=(SELECT role_id FROM `sp_manager` WHERE mg_name='" + global.UserName + "' LIMIT 1) AND ps_ids LIKE '%167%';", function (error, results, fields) {
			if (error) throw error;
			x = results[0].num;
			if(global.UserName == 'admin' || x == 1) {
			orderServ.getAllProcess(
			conditions,
			function(err,result){
				if(err) return res.sendResult(null,400,err);
				res.sendResult(result,200,"获取成功");
			}
		)(req,res,next);
		}else{
			global.flug = 1;
			orderServ.getProcessUser(
			conditions,
			function(err,result){
				if(err) return res.sendResult(null,400,err);
				res.sendResult(result,200,"获取成功");
			}
		)(req,res,next);
		}
		});
	}
);










// 更新送检状态
router.put("/:id",
	// 参数验证
	function(req,res,next) {
		next();
	},
	// 业务逻辑
	function(req,res,next) {
		var params = req.body;
		
		// 加载配置文件
		var config = require('config').get("db_config");
	
		var connection = mysql.createConnection({
			host     : config.get("host"),
			user     : config.get("user"),
			password : config.get("password"),
			database : config.get("database")
		});
		connection.connect();


		var x = 0;
		connection.query("SELECT COUNT(*) as num FROM `sp_role` WHERE role_id=(SELECT role_id FROM `sp_manager` WHERE mg_name='" + global.UserName + "' LIMIT 1) AND ps_ids LIKE '%162%';", function (error, results, fields) {
			if (error) throw error;
			x = results[0].num;
			if(global.UserName == 'admin' || x == 1) {
			orderServ.updateProcess(req.params.id,params.yiqi_zhuangtai,function(err,process){
			if(err) return res.sendResult(null,400,err);
			return res.sendResult(process,200,"更新流转状态成功");
		})(req,res,next);
		}else{
			return res.sendResult(null,401,"更新流转状态失败!无权限");
		}
		});
		
		
	}
);

router.get("/:id",function(req,res,next){
	orderServ.getProcess(req.params.id,function(err,result){
		if(err) return res.sendResult(null,400,err);
		return res.sendResult(result,200,"获取成功");
	})(req,res,next);
});






/**

**/

// 删除角色
router.delete("/:id",	
	// 业务逻辑
	function(req,res,next) {
		orderServ.deleteProcess(req.params.id,function(err){
			if(err)
				return res.sendResult(null,400,"删除失败");
			else
				return res.sendResult(null,200,"删除成功");
		})(req,res,next);
	}
);



module.exports = router;
